//
//  VerRecordController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/7.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "VerRecordController.h"
#import "VerRecordCell.h"
#import "VerRecordTCell.h"

@interface VerRecordController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *billData;
@property (nonatomic, assign) NSInteger PageNumber;
@property (nonatomic,strong)NSNumber *totalCountKey;


@end

@implementation VerRecordController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"核销记录";
    
    _billData  = [NSMutableArray array];
    
    [self setUI];
    [self loadData];
    [self loadReferView];
}
- (void)loadData{
    
    _PageNumber = 1;
    __weak __typeof(&*self)weakSelf = self;
    [SCApiTool ScWriteofflistStartTime:@"" endTime:@"" curPage:_PageNumber pageSize:2 Success:^(NSDictionary *data) {
        weakSelf.totalCountKey = [data hw_safeObjectForKey:@"countPage"];
        NSArray *bill = [data hw_safeObjectForKey:@"codes"];
        if (bill.count == 0) {
            [SCTool showMessage:@"暂无数据"];
        }
        weakSelf.billData = bill.mutableCopy;
        [weakSelf performSelector:@selector(setBillData:) withObject:_billData];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSString *errormsg) {
        [SCTool showError:errormsg];
        
    }];
}
- (void)loadReferView
{
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)loadMoreData{
    _PageNumber++;
    if ([_totalCountKey integerValue]< _PageNumber) {
        [SCTool showMessage:@"没有更多数据"];
        [_tableView.mj_footer endRefreshing];
        _PageNumber--;
        return ;
    }
    __weak __typeof(&*self)weakSelf = self;

    [SCApiTool ScWriteofflistStartTime:@"" endTime:@"" curPage:_PageNumber pageSize:2 Success:^(NSDictionary *dic) {
        
        [weakSelf.billData addObjectsFromArray:[dic hw_safeObjectForKey:@"codes"]];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_footer endRefreshing];
        
    } failure:^(NSString *errormsg) {
        [SCTool showMessage:errormsg];
        [weakSelf.tableView.mj_footer endRefreshing];
        
    }];

}

- (void)setBillData:(NSMutableArray *)billData{
    _billData = billData;
    if ([_billData count] < 2) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        [self.tableView.mj_footer setHidden:NO];
    }
    
}


- (void)setUI{
    
    [self.view addSubview:self.tableView];
    __weak __typeof(&*self)weakSelf = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(weakSelf.view);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _billData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *typeDic = _billData[indexPath.row];
    if ([[typeDic hw_safeObjectForKey:@"writeoff_status"] integerValue] == 1) {
        VerRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identify" forIndexPath:indexPath];
        cell.dic = _billData[indexPath.row];
        return cell;
    }else{
        VerRecordTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VerRecordTCell" forIndexPath:indexPath];
        cell.dic = _billData[indexPath.row];
        return cell;
    }
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *typeDic = _billData[indexPath.row];
    if ([[typeDic hw_safeObjectForKey:@"writeoff_status"] integerValue] == 1) {
        return 85;
    }else{
        return 105;
    }
    return 85;
}

- (UITableView *)tableView{
    
    if (_tableView == nil) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorColor = WYColor(238, 238, 238);
        tableView.tableFooterView = [[UIView alloc] init];
        [tableView registerNib:[UINib nibWithNibName:@"VerRecordCell" bundle:nil] forCellReuseIdentifier:@"identify"];
        [tableView registerNib:[UINib nibWithNibName:@"VerRecordTCell" bundle:nil] forCellReuseIdentifier:@"VerRecordTCell"];
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


@end
